import { findAllJob, validatorCron, setJobStatus, delJob, batchSetJobStatus, batchDelJob, addJob } from '../services/alarm-job-controller';

export default {
  namespace: 'JobModel',

  state: {
    queryParam: {
      jobName: undefined,
    },
    data: [],
    selectedRowKeys: [],
    cronTimeData: [],

    // 基本配置
    addJobConfigData: {
      name: undefined,
      cron: '*/5 * * * * ?',
      rule: 'any',
      frequencyTimeLength: undefined,
      frequencyTimeLengthUnit: undefined,
      frequencyCountMax: undefined,
      flatlineTimeLengthMax: undefined,
      flatlineTimeLengthMaxUnit: undefined,
      description: undefined,
    },
    // 监控项
    esMetricDataList: [],
    // 告警通知
    notificationConfigList: [],
  },

  effects: {
    *findAllJob({ payload }, { select, call, put }) {
      let queryParam = yield select(state => state.JobModel.queryParam);
      queryParam = { ...queryParam, ...payload }
      // 请求数据
      const data = yield call(findAllJob, queryParam);
      if (!data) return;
      // 保存数据
      yield put({ type: 'save', payload: { data, queryParam } });
    },
    *validatorCron({ payload }, { call, put }) {
      const { cron, num } = payload;
      // 请求数据
      const cronTimeData = yield call(validatorCron, { cron, num });
      if (!cronTimeData) return;
      // 保存数据
      yield put({ type: 'save', payload: { cronTimeData } });
    },
    *setJobStatus({ payload, successCallBack }, { call, put }) {
      const { name, status } = payload;
      // 请求数据
      const result = yield call(setJobStatus, name, { status });
      if (!result) return;
      // 刷新数据
      yield put({ type: 'findAllJob' });
      if (successCallBack instanceof Function) successCallBack(result);
    },
    *batchSetJobStatus({ payload, successCallBack }, { call, put }) {
      const { names, status } = payload;
      // 请求数据
      const result = yield call(batchSetJobStatus, { names, status });
      if (!result) return;
      // 刷新数据
      yield put({ type: 'save', payload: { selectedRowKeys: [] } });
      yield put({ type: 'findAllJob' });
      if (successCallBack instanceof Function) successCallBack(result);
    },
    *delJob({ payload, successCallBack }, { call, put }) {
      const { name } = payload;
      // 请求数据
      const result = yield call(delJob, name);
      if (!result) return;
      // 刷新数据
      yield put({ type: 'findAllJob' });
      if (successCallBack instanceof Function) successCallBack(result);
    },
    *batchDelJob({ payload, successCallBack }, { call, put }) {
      const { names } = payload;
      // 请求数据
      const result = yield call(batchDelJob, names);
      if (!result) return;
      // 刷新数据
      yield put({ type: 'save', payload: { selectedRowKeys: [] } });
      yield put({ type: 'findAllJob' });
      if (successCallBack instanceof Function) successCallBack(result);
    },
    *addJob({ payload, successCallBack }, { call, put }) {
      // 请求数据
      const result = yield call(addJob, payload);
      if (!result) return;
      // 刷新数据
      yield put({ type: 'save', payload: { esMetricDataList: [], notificationConfigList: [] } });
      if (successCallBack instanceof Function) successCallBack(result);
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
